<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="自习室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zixishiId">
              <j-dict-select-tag :disabled="canEdit" type="list" v-model="model.zixishiId" dictCode="sr_zixishi,name,id" placeholder="请选择自习室" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tag">
              <!-- <a-input v-model="model.tag" placeholder="请输入标签" ></a-input> -->
              <a-select
                v-model:value="model.tag"
                mode="tags"
                :token-separators="[',']"
                placeholder="请输入标签"
              ></a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="quyu_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="区域logo" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">
              <j-image-upload isMultiple :number=1 v-model="model.logo" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24" >
            <a-form-model-item label="区域介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <j-editor v-model="model.remark" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24" >
            <a-form-model-item label="小时价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timePrice">
              <a-input v-model="model.timePrice" placeholder="请输入价格" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="一天价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dayPrice">
              <a-input v-model="model.dayPrice" placeholder="请输入价格" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devices">
              <j-multi-select-tag type="select" v-model="model.devices" dictCode="sr_device,device_name,uid" placeholder="请关联设备" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="轮播图" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="swiperPicTable.loading"
          :columns="swiperPicTable.columns"
          :dataSource="swiperPicTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="座位" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="seatTable.loading"
          :columns="seatTable.columns"
          :dataSource="seatTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="优惠券" :key="refKeys[2]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[2]"
          :loading="couponTable.loading"
          :columns="couponTable.columns"
          :dataSource="couponTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
          
      </a-tab-pane>
    </a-tabs>
    
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'QuyuForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        canEdit: true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['swiperPic', 'seat','coupon', ],
        tableKeys:['swiperPic', 'seat','coupon', ],
        activeKey: 'swiperPic',
        // 轮播图
        swiperPicTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '图片标题',
              key: 'title',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '图片路径',
              key: 'path',
              type: JVXETypes.image,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 座位
        seatTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '名称',
              key: 'name',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '编码',
              key: 'code',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '图片',
              key: 'pic',
              type: JVXETypes.image,
              token:true,
              responseName:"message",
              number: 1,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态',
              key: 'status',
              type: JVXETypes.select,
              options:[],
              dictCode:"seat_status",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 优惠券
        couponTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '名称',
              key: 'name',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '类型',
              key: 'type',
              type: JVXETypes.select,
              options:[],
              dictCode:"coupon_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '时长',
              key: 'duration',
               type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '原金额',
              key: 'origAmt',
               type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '优惠金额',
              key: 'disAmt',
               type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '数量',
              key: 'cnt',
               type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '有效期开始时间',
              key: 'effectStartDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '有效期结束时间',
              key: 'effectEndDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '开始使用时间',
              key: 'useStartTime',
              type: JVXETypes.time,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '结束使用时间',
              key: 'useEndTime',
              type: JVXETypes.time,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '有效天数',
              key: 'days',
               type: JVXETypes.input,
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '状态',
              key: 'status',
              type: JVXETypes.select,
              options:[],
              dictCode:"coupon_status",
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/zixishi/quyu/add",
          edit: "/zixishi/quyu/edit",
          queryById: "/zixishi/quyu/queryById",
          swiperPic: {
            list: '/zixishi/quyu/querySwiperPicByMainId'
          },
          seat: {
            list: '/zixishi/quyu/querySeatByMainId'
          },
          coupon: {
            list: '/zixishi/quyu/queryCouponByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.swiperPicTable.dataSource=[]
        this.seatTable.dataSource=[]
        this.couponTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.swiperPic.list, params, this.swiperPicTable)
          this.requestSubTableData(this.url.seat.list, params, this.seatTable)
          this.requestSubTableData(this.url.coupon.list, params, this.couponTable)
        }
        
        if(this.model.id){
          this.canEdit = true;
        }else{
          this.canEdit = false;
        }
        
        let tags = this.model.tag;
        if(tags){
          this.model.tag = tags.split(',');
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        let tags = main.tag;
        if(tags){
          main.tag = tags.join(',')
        }
        return {
          ...main, // 展开
          swiperPicList: allValues.tablesValue[0].tableData,
          seatList: allValues.tablesValue[1].tableData,
          couponList: allValues.tablesValue[2].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>